/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import units.CellType;
import units.MonkeyType;
import utils.FileUtils;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class MakeClustersTable_individual {

    private double membershipCutoff = 0.8;
    private int otherMemberId = 0;
    private int maxNoOfClusters = 20;

    private File clustersFolder = new File("/Users/jamie/monkeys/clust/clusters");
    private TreeMap<Integer, Double> bestCoefficients;
    private TreeMap<Integer, String> membership;

    public void fillTables(){
        
        Handle handle = Handle.getConnectedHandle("monkeys");
        for(MonkeyType mt : MonkeyType.values()){
            for(CellType ct : CellType.values()){
                bestCoefficients = new TreeMap<Integer, Double>();
                membership = new TreeMap<Integer, String>();
                for(int i=1; i<=maxNoOfClusters;i++){
                    String fileName = "POST_"+mt.name()+"_"+ct.name()+"_"+i+".txt";
                    File f = new File(clustersFolder, fileName);
                    if(f.exists()){
                        process(handle, f, ct, mt, i);
                    }
                }
                processMembers(handle, bestCoefficients, membership, mt, ct);
            }
        }
        handle.closeConnection();
    }

    private void process(Handle handle, File f, CellType cellType, MonkeyType monkeyType, int clusterNo) {
        BufferedReader reader = FileUtils.getBufferedReader(f);
        String line;
        String clusterId = monkeyType.name()+cellType.name()+clusterNo;
        try {
            while ((line = reader.readLine()) != null) {
                String[] split = line.split("\t");
                int probeId = Integer.parseInt(split[0]);
                double coeff = Double.parseDouble(split[1]);
                if(!bestCoefficients.containsKey(probeId)){
                    if(coeff>membershipCutoff){
                        bestCoefficients.put(probeId, coeff);
                        membership.put(probeId, clusterId);
                    } else {
                        bestCoefficients.put(probeId, coeff);
                        membership.put(probeId, monkeyType.name()+cellType.name()+otherMemberId);
                    }
                } else {
                    if(bestCoefficients.get(probeId)<coeff){
                        if(coeff>membershipCutoff){
                            bestCoefficients.put(probeId, coeff);
                            membership.put(probeId, clusterId);
                        }else {
                            bestCoefficients.put(probeId, coeff);
                            membership.put(probeId, monkeyType.name()+cellType.name()+otherMemberId);
                        }
                    }
                }
                insert(handle, "clusterData", clusterId,  probeId, coeff, monkeyType, cellType);
            }
            reader.close();
        } catch (IOException ex) {
            Logger.getLogger(MakeClustersTable_individual.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void makeClusterDataTable(){
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql = "drop table if exists clusterData";
        handle.runSQLupdate(sql);
        sql = "create table clusterData (id int(10) primary key auto_increment, clusterId varchar(5) not null, probeId int(10) not null, coefficient double not null, monkeyType char(1) not null, cellType char(1) not null)";
        handle.runSQLupdate(sql);
        sql = "create index clusterId_index on clusterData(clusterId)";
        handle.runSQLupdate(sql);
        sql = "create index probeId_index on clusterData(probeId)";
        handle.runSQLupdate(sql);
        handle.closeConnection();
    }

    public void makeClusterMembershipTable(){
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql = "drop table if exists clusterMembership";
        handle.runSQLupdate(sql);
        sql = "create table clusterMembership (id int(10) primary key auto_increment, clusterId varchar(5) not null, probeId int(10) not null, coefficient double not null, monkeyType char(1) not null, cellType char(1) not null)";
        handle.runSQLupdate(sql);
        sql = "create index clusterId_index on clusterMembership(clusterId)";
        handle.runSQLupdate(sql);
        sql = "create index probeId_index on clusterMembership(probeId)";
        handle.runSQLupdate(sql);
        handle.closeConnection();
    }

    private void insert(Handle handle, String tableName, String clusterId, int probeId, double coeff, MonkeyType mt, CellType ct) {
        
        String sql = "insert into "+tableName+" values(0,'"+clusterId+"', "+probeId+", "+coeff+", '"+mt.name()+"', '"+ct.name()+"')";
        handle.runSQLupdate(sql);
    }

    private void processMembers(Handle h, TreeMap<Integer, Double> bestCoefficients, TreeMap<Integer, String> membership, MonkeyType monkeyType, CellType cellType) {
        for(int probeId : bestCoefficients.keySet()){
            double coeff = bestCoefficients.get(probeId);
            String clusterId = membership.get(probeId);
            insert(h, "clusterMembership", clusterId, probeId, coeff, monkeyType, cellType);
        }
    }



}
